

def next_down(current, widget):
    if current.active(): return current
    
    hd = None
    omin = current.rect[1] - widget.rect[1]
    dmin = current.rect[0] + current.rect[2] + widget.rect[0] + widget.rect[2]
    
    if current.children:
        for jd, truc in current.children:
            kd = next_down(jd, widget)
            if kd != None and kd != widget:
                o = abs(widget.rect[1] + widget.rect[3] - kd.rect[1])
                d = abs(widget.rect[0] - kd.rect[0])
                
                if o >= 0 and o <= omin and d <= dmin:
                    hd = kd
                    omin = o
                    dmin = d
    
    return hd
